﻿
Imports MeoTom.Core

Partial Public Class PageManager
    Inherits UI.Page
    Public a As New BLL.Pages()
    Public util2 As New Utilities.Common()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim previousPage As String = ""
        If Request.QueryString.ToString = vbNullString Then
            previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
        Else
            previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
        End If
        util2.ValidateLogin(previousPage)

        If Request.QueryString("action") = "Delete" Then
            If Request.QueryString("ID") <> vbNullString Then
                DeleteArticle(Request.QueryString("ID"))
                Response.Redirect("/mt-inside/PageManager.aspx", False)
            End If
        Else
            BindGrid()
        End If
    End Sub

    Public Sub BindGrid()
        grdListPages.DataSource = a.GetAll()
        grdListPages.DataBind()
    End Sub

    Public Function DisplayStatus(ByVal b As Boolean)
        Dim str As String = ""
        If Not b Then
            str = " - <b>Draft</b>"
        End If
        Return str
    End Function

    Public Function DisplayStatusDate(ByVal b As Boolean, ByVal d As String)
        Dim str As String = ""
        Dim dDate As DateTime = DateTime.Parse(d).AddHours(Configs.TimeLine)
        If Not b Then
            str = String.Format("{0:dd/MM/yyyy}", dDate) & "<br />Draft"
        Else
            If dDate > DateTime.Now Then
                str = String.Format("{0:dd/MM/yyyy}", dDate) & "<br />Scheduled"
            Else
                str = String.Format("{0:dd/MM/yyyy}", dDate) & "<br />Published"
            End If
        End If
        Return str
    End Function

    Private Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Const i As Integer = 0
        If Request.Form("selectedAction0") = "1" Or Request.Form("selectedAction1") = "1" Then
            'Edit Category
            Dim vId As String = ""
            For Each str As String In From str1 In Regex.Split(Request.Form("cboCheckArticle"), ",") Where i = 0
                vId = str
                Exit For
            Next
            Response.Redirect("/mt-inside/PageUpdate.aspx?ID=" & vId, False)
        ElseIf Request.Form("selectedAction0") = "2" Or Request.Form("selectedAction1") = "2" Then
            'Delete
            For Each str As String In Regex.Split(Request.Form("cboCheckArticle"), ",")
                DeleteArticle(str)
            Next
            a.WriteTop10Last()
            BindGrid()
        End If
    End Sub

    Private Sub btnApply2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply2.Click
        btnApply_Click(sender, e)
    End Sub

    Public Sub DeleteArticle(ByVal PageID As String)
        a.PageID = PageID
        a.GetByEdit()

        util2.DeleteFile(Configs.PageFile & "ListFile\" & a.CreatedDate.Year.ToString() & "\" & a.CreatedDate.Month.ToString() & "\" & a.CreatedDate.Day.ToString() & "\" & a.PageID & ".xml")

        a.PageID = PageID
        a.Delete()
    End Sub

    Private Sub grdListPages_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles grdListPages.PageIndexChanging
        grdListPages.PageIndex = e.NewPageIndex
        BindGrid()
    End Sub
End Class